/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author daz
 */
public class ReadDatFile {

    private int id;

    public void read() {
    }

    public int getId() {
        return id;
    }

    public void read(String str) {
        InputStream is = PiPoDesigner.class.getResourceAsStream(str);
        try {
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            Debug.d("buffer len:" + buffer.length);
            byte[] posByte = new byte[4];
            byte[] idbyte = new byte[4];
            System.arraycopy(buffer, buffer.length - 5, posByte, 0, 4);
            int pos = byteArrayToInt(posByte);
            Debug.d("pos :" + pos);
            System.arraycopy(buffer, pos, idbyte, 0, 4);
            this.id = byteArrayToInt(idbyte);
            Debug.d("id :" + byteArrayToInt(idbyte));
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }

    public int byteArrayToInt(byte[] b) {
        return (b[0] << 24)
                + ((b[1] & 0xFF) << 16)
                + ((b[2] & 0xFF) << 8)
                + (b[3] & 0xFF);
    }

    static byte[] intToByteArray(int value) {
        return new byte[]{
                    (byte) (value >>> 24),
                    (byte) (value >>> 16),
                    (byte) (value >>> 8),
                    (byte) value};
    }

    public int toInt(byte[] bytes) {
        int result = 0;
        for (int i = 0; i < 4; i++) {
            result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
        }
        return result;
    }
}
